package com.olympus.hermes.auth.controller;

import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import com.olympus.hermes.common.annotation.IgnoreResponseAdvice;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;

/**
 * @author Mickey
 * @since 2021/1/30
 */
@RestController
@AllArgsConstructor
public class RsaController {

    private final KeyPair keyPair;

    @IgnoreResponseAdvice
    @GetMapping("/rsa/publicKey")
    public Object publicKey() {
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey key = new RSAKey.Builder(publicKey).build();
        return new JWKSet(key).toJSONObject();
    }
}
